package com.thinta.wind.ui.home.fragment

import android.view.LayoutInflater
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.SimpleItemAnimator
import com.chad.library.adapter.base.listener.OnLoadMoreListener
import com.stx.xhb.androidx.XBanner
import com.thinta.wind.R
import com.thinta.wind.base.BaseFragment
import com.thinta.wind.databinding.FollowBannerViewBinding
import com.thinta.wind.databinding.FollowFragmentBinding
import com.thinta.wind.databinding.FollowHeaderViewBinding
import com.thinta.wind.entity.HomeRecommendItemListBean
import com.thinta.wind.entity.RecommendBannerItem
import com.thinta.wind.ui.home.adapter.FollowAdapter
import com.thinta.wind.ui.home.repository.FollowModelFactory
import com.thinta.wind.ui.home.repository.FollowRepository
import com.thinta.wind.ui.home.viewmodel.FollowViewModel

class FollowFragment : BaseFragment<FollowViewModel, FollowFragmentBinding>() {

    companion object {
        fun newInstance() = FollowFragment()
    }

    private var pageIndex = 1

    private lateinit var mFollowList : MutableList<HomeRecommendItemListBean>

    private lateinit var headBannerView: View

    private lateinit var footerView : View

    private lateinit var mBannerBinding : FollowHeaderViewBinding

    private lateinit var mBannerViewBinding: FollowBannerViewBinding

    private val mFollowAdapter : FollowAdapter by lazy { FollowAdapter(mFollowList) }

    private val mAdapter : XBanner.XBannerAdapter by lazy { getBannerAdapter() }

    override fun getModelFactory(): ViewModelProvider.NewInstanceFactory = FollowModelFactory(
        FollowRepository()
    )

    override fun viewModelClass(): Class<FollowViewModel> = FollowViewModel::class.java

    override fun getLayoutId(): Int = R.layout.follow_fragment

    override fun afterCreate() {
        initData()
        initView()
        initListener()
    }

    private fun initView(){
        dataBinding?.followList?.layoutManager = LinearLayoutManager(context)
        dataBinding?.followList?.isNestedScrollingEnabled = true
        (dataBinding?.followList?.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
        dataBinding?.followList?.adapter = mFollowAdapter
        headBannerView = LayoutInflater.from(context).inflate(R.layout.follow_header_view, null)
        footerView = LayoutInflater.from(context).inflate(R.layout.follow_footer_view, null)
        mBannerBinding = initViewBinding(headBannerView.rootView) as FollowHeaderViewBinding
        mBannerBinding.headerTitle.text = resources.getString(R.string.home_recommend_wonderful)
        mFollowAdapter.addHeaderView(headBannerView)
    }

    private fun initData(){
        mFollowList = mutableListOf()
        initBannerData()
        initFollowList(pageIndex)
    }

    private fun initBannerData(){
        viewModel?.getFollowBanner()
        viewModel?.bannerResult?.observe(this, {
            mBannerBinding.headerPager.setBannerData(R.layout.follow_banner_view, it)
            mBannerBinding.headerPager.loadImage(mAdapter)
        })
    }

    private fun initFollowList(index : Int){
        viewModel?.getFollowList(index)
        viewModel?.result?.observe(this, {
            mFollowList.addAll(it)
            mFollowAdapter.notifyDataSetChanged()
            if(index > 1) mFollowAdapter.loadMoreModule.loadMoreComplete()
        })
    }

    private fun initListener(){
        mFollowAdapter.loadMoreModule.setOnLoadMoreListener {
            ++pageIndex
            if (pageIndex <= 5) {
                initFollowList(pageIndex)
            } else {
                mFollowAdapter.loadMoreModule.loadMoreEnd(true)
                mFollowAdapter.addFooterView(footerView)
            }
        }
        mFollowAdapter.loadMoreModule.isAutoLoadMore = true
    }

    private fun getBannerAdapter() : XBanner.XBannerAdapter = XBanner.XBannerAdapter { _, model, view, _ ->
        mBannerViewBinding = initViewBinding(view) as FollowBannerViewBinding
        mBannerViewBinding.bannerItem = model as RecommendBannerItem
    }

}
